let Exceltotable = (function () {

    return function Exceltotable(e, el) {
        const file = e.target.files[0];
        LuckyExcel.transformExcelToLucky(
            file,
            function (exportJson, luckysheetfile) {
                CreateTable(exportJson, el);

            },
            function (err) {
                logger.error('Import failed. Is your fail a valid xlsx?');
            }
        );
        e.target.value = null;
    }

})();
let Exceltotableurl = (function () {
    return function Exceltotable(url, name, el) {
        LuckyExcel.transformExcelToLuckyByUrl(
            url, name,
            function (exportJson, luckysheetfile) {
                CreateTable(exportJson, el);
            },
            function (err) {
                logger.error('Import failed. Is your fail a valid xlsx?');
            }
        );

    }

})();
let CreateTable = (function () {
    return function CreateTable(exportJson, el) {
        function fontSizeInPixels(fontSizeInPoints) { //把字体磅转成像素的函数，可以根据不同的显示器转化
            function js_getDPI() {
                var arrDPI = new Array();
                if (window.screen.deviceXDPI != undefined) {
                    arrDPI[0] = window.screen.deviceXDPI;
                    arrDPI[1] = window.screen.deviceYDPI;
                } else {
                    var tmpNode = document.createElement("DIV");
                    tmpNode.style.cssText = "width:1in;height:1in;position:absolute;left:0px;top:0px;z-index:99;visibility:hidden";
                    document.body.appendChild(tmpNode);
                    arrDPI[0] = parseInt(tmpNode.offsetWidth);
                    arrDPI[1] = parseInt(tmpNode.offsetHeight);
                    tmpNode.parentNode.removeChild(tmpNode);
                }
                return (arrDPI[0] + arrDPI[1]) / 2;
            }
            let pointsPerInch = 72;
            let pixelsPerInch = js_getDPI();
            let pixelsPerPoint = pixelsPerInch / pointsPerInch;

            return fontSizeInPoints * pixelsPerPoint;
        }

        function getRowCol(cellId) { //A1转坐标
            var colStr = cellId.replace(/\d/g, ''); // 提取字母列标识
            var row = parseInt(cellId.replace(/[a-zA-Z]/g, '')); // 提取数字行标识

            var col = 0;
            for (var i = 0; i < colStr.length; i++) {
                col += (colStr.charCodeAt(i) - 64) * Math.pow(26, colStr.length - i - 1);
            }
            return [row - 1, col - 1];
        }
        const alignment = {
            vt: {
                0: 'middle',
                1: 'up',
                2: 'down'
            },
            ht: { //text-align
                0: 'center',
                1: 'left',
                2: 'right'
            },
            tr: {
                0: 0,
                1: 45,
                2: -45,
                3: 'vertical',
                4: 90,
                5: -90
            },
            tb: {
                0: 'truncation',
                1: 'overflow',
                2: 'word wrap'
            }
        }
        var borderTypes = {
            0: "none",
            1: "thin",
            2: "hair",
            3: "dotted",
            4: "dashed",
            5: "dashDot",
            6: "dashDotDot",
            7: "double",
            8: "medium",
            9: "mediumDashed",
            10: "mediumDashDot",
            11: "mediumDashDotDot",
            12: "slantDashDot",
            13: "thick"
        };
       console.log(exportJson);
        let sheet = exportJson.sheets[0];
        let columnlen = sheet.config.columnlen;
        let rowlen = sheet.config.rowlen;
        let starti = getRowCol(sheet.config.dimension.split(":")[0]);
        let endj = getRowCol(sheet.config.dimension.split(":")[1]);
        let table = document.createElement("table");
        table.setAttribute("cellspacing", "0");
        table.style.borderCollapse = "collapse";
        let tbody = document.createElement("tbody");
        table.appendChild(tbody);
        for (i = 0; i < endj[0] + 1; i++) {
            let tr = document.createElement("tr");
            tbody.appendChild(tr);
            for (j = 0; j < endj[1] + 1; j++) {
                let rowh;
                let colw;
                if (rowlen && rowlen[i]) {
                    rowh = rowlen[i];
                } else {
                    rowh = sheet.defaultRowHeight;
                }
                if (columnlen && columnlen[j]) {
                    colw = columnlen[j]
                } else {
                    colw = sheet.defaultColWidth;
                }
                let td = document.createElement("td");
                td.setAttribute("data-cell", i + " " + j);
                td.style.height = rowh + "px";
                td.style.width = colw + "px";
                tr.appendChild(td);
            }

        }
        //赋值
        let tabledata = sheet.celldata;
        for (let cell in tabledata) {
            let tdvalue;
            if (tabledata[cell].v.v) {
                tdvalue = tabledata[cell].v.v;
            } else {
                tdvalue = "";
            }
            if (tabledata[cell].v.ct.s) {
                for (let s of tabledata[cell].v.ct.s) {
                    tdvalue += s.v;
                }
            }
            let ht;
            if (tabledata[cell].v.ht !== "undefined") {
                ht = alignment.ht[tabledata[cell].v.ht];
            }
            let td = table.querySelector(`td[data-cell="${tabledata[cell].r} ${tabledata[cell].c}"]`);
            td.innerText = tdvalue;
            td.style.textAlign = ht;
            let fs;
            let ff;
            if (tabledata[cell].v.fs !== "undefined" && tabledata[cell].v.ff !== "undefined") {
                fs = fontSizeInPixels(tabledata[cell].v.fs);
                ff = tabledata[cell].v.ff;
            }
            td.style.fontSize = fs / 16 + "em";
            td.style.fontFamily = ff;
            if (tabledata[cell].v.bl) {
                td.style.fontWeight = "bold";

            }
            if (tabledata[cell].v.fc) {
                td.style.color = tabledata[cell].v.fc;
            }
        }

        //设置边框
        let borderInfo = sheet.config.borderInfo;
        for (let cell of borderInfo) {
            let td = table.querySelector(`td[data-cell="${cell.value.row_index} ${cell.value.col_index}"]`);
            if (cell.value.b) td.style.borderBottom = borderTypes[cell.value.b.style] + " solid " + cell.value.b.color;
            if (cell.value.l) td.style.borderLeft = borderTypes[cell.value.l.style] + " solid " + cell.value.l.color;
            if (cell.value.t) td.style.borderTop = borderTypes[cell.value.t.style] + " solid " + cell.value.t.color;
            if (cell.value.r) td.style.borderRight = borderTypes[cell.value.r.style] + " solid " + cell.value.r.color;
        }
        //合并单元格
        let merge = sheet.config.merge;
        for (let cell in merge) {
            let td = table.querySelector(`td[data-cell="${merge[cell].r} ${merge[cell].c}"]`);
            // 设置跨行跨列
            td.colSpan = merge[cell].cs;
            td.rowSpan = merge[cell].rs;

            // 隐藏其他单元格
            for (let row = merge[cell].r; row < merge[cell].r + merge[cell].rs; row++) {
                for (let col = merge[cell].c; col < merge[cell].cs + merge[cell].c; col++) {
                    if (row > merge[cell].r || col > merge[cell].c) {
                        if (col === merge[cell].cs + merge[cell].c - 1 && row == merge[cell].r + merge[cell].rs - 1) {
                            td.style.borderBottom = table.querySelector(`td[data-cell="${row} ${col}"]`).style.borderBottom;
                            td.style.borderRight = table.querySelector(`td[data-cell="${row} ${col}"]`).style.borderRight;
                        }
                        table.querySelector(`td[data-cell="${row} ${col}"]`).style.display = 'none';
                    }
                }
            }
        }
        el.innerHTML = "";//清空
        el.appendChild(table);
    }
})();